package jp.co.sharp.android.parents.kidsguard.activity.edit.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.RadioButton;
import android.widget.TextView;

import com.android.bluetooth.parentble.ParentBleTransfer;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import jp.co.sharp.android.parents.kidsguard.R;
import jp.co.sharp.android.parents.kidsguard.entity.KidInfoEntity;
import jp.co.sharp.android.parents.kidsguard.thread.BusinessThread;

public class AlramListAdapter extends ArrayAdapter<String> {
    private int resourceId;
    // 用于记录每个RadioButton的状态，并保证只可选一个
    Map<String, Integer> ringtoneMap;
    private String voiceName;
    private KidInfoEntity mKidInfoEntity;
    public ParentBleTransfer mParentBleTransfer;



    public AlramListAdapter(@NonNull Context context, int resource) {
        super(context, resource);
        this.resourceId = resource;
    }

    public AlramListAdapter(@NonNull Context context, int resource, @NonNull List<String> objects) {
        super(context, resource, objects);
        this.resourceId = resource;
    }

    @Override
    public View getView(int position, View converView, ViewGroup parent) {
        String thisVoiceName = getItem(position);

        View view;
        AlramListAdapter.ViewHolder viewHolder;
        if (null == converView) {
            view = LayoutInflater.from(getContext()).inflate(resourceId, parent, false);

            viewHolder = new AlramListAdapter.ViewHolder();
            viewHolder.voiceRadio = view.findViewById(R.id.voice_radio_button);

            view.setTag(viewHolder);
        } else {
            view = converView;
            viewHolder = (AlramListAdapter.ViewHolder) view.getTag();
        }
        viewHolder.voiceRadio.setText(thisVoiceName);
        viewHolder.voiceRadio.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 重置，确保最多只有一项被选中
                voiceName = thisVoiceName;
                AlramListAdapter.this.notifyDataSetChanged();
                new BusinessThread(getPlayRingtoneJson(ringtoneMap.get(voiceName)),mParentBleTransfer,mKidInfoEntity).start();
            }
        });
        boolean res = true;
        if (voiceName == null || !voiceName.equals(thisVoiceName)) {
            res = false;
        }
        viewHolder.voiceRadio.setChecked(res);
        return view;
    }

    public String getVoiceName() {
        return this.voiceName;
    }

    public void setVoiceName(String voiceName) {
        this.voiceName = voiceName;
    }

    public Map<String, Integer> getRingtoneMap() {
        return ringtoneMap;
    }

    public void setRingtoneMap(Map<String, Integer> ringtoneMap) {
        this.ringtoneMap = ringtoneMap;
    }

    class ViewHolder {
        RadioButton voiceRadio;
    }

    private JSONObject getPlayRingtoneJson(int position) {
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("method", "playRingtone");
            JSONObject data=new JSONObject();
            data.put("position",position);
            jsonObject.put("data", data);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return jsonObject;
    }
    public void setmKidInfoEntity(KidInfoEntity mKidInfoEntity) {
        this.mKidInfoEntity = mKidInfoEntity;
    }

    public void setmParentBleTransfer(ParentBleTransfer mParentBleTransfer) {
        this.mParentBleTransfer = mParentBleTransfer;
    }
}
